package com.qf.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;

/**
 * @author sunyamei
 * @version V1.0
 * @Project springmvc_Demo
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/7/14 15:40
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @RequestMapping("/upload")
    public  String upload(String name, MultipartFile file, HttpSession session){
        try {
        String basePath = session.getServletContext().getRealPath("/upload");
        File dirs = new File(basePath);
        if (!dirs.exists()){
            dirs.mkdir();
        }
        //获取上传组件的name值
       String filename= file.getOriginalFilename();//file name 名称 101.jsp
        //完成文件上传,将指定图片上传到指定路径的位置
        file.transferTo(new File(basePath+File.separator+filename));//upload/101.jsp

            return  "success";
        } catch (Exception e) {
            e.printStackTrace();
            return  "fail";
        }
    }

}
